<?php

/**
 * 首页
 * @copyright (c) 2013, Mr.Wang
 * @author Mr.Wang <281762391@qq.com>
 */
class InfoAction extends UserAction {

    public function index() {
        $uid  = session('uid');
        //个人资料
        $this->Model = D("User");
        $user = $this->Model->find($uid);
        //实名认真资料
        $ver  = M('UserVerified')->where("user_id={$uid}")->find();

        //推荐信息
        $roundDZ = _roundDazhe();
        $this->assign('rounddz', $roundDZ);
        $this->assign('user', $user);
        $this->assign('ver', $ver);
        $this->display();
    }

    public function password() {
        $this->Model = D("User");
        $user = $this->Model->find(session('uid'));
        $this->assign("user", $user);

        if (IS_POST) {
            if ($user['password'] != md5($_POST['oldpassword'])) {
                $this->error("原始密码错误,请重新输入！");
            }
            $_POST['user_id'] = session('uid');
            if ($this->Model->create()) {
                if ($this->Model->save()) {
                    $this->success("修改成功！", U("info/password"));
                } else {
                    $this->error("修改失败！");
                }
            } else {
                $this->error($this->Model->getError());
            }
        } else {
            $user = $this->Model->find(session('uid'));

            //推荐信息
            $roundDZ = _roundDazhe();
            $this->assign('rounddz', $roundDZ);
            $this->display();
        }
    }

    public function address() {
        $this->Model = D("UserExt");
        $user_ext = $this->Model->find(session('uid'));
        if (!$user_ext) {
            $addData = array("uid" => session('uid'));
            $this->Model->add($addData);
        }

        $area = array_filter(explode('_', $user_ext['user_area']));

        $this->assign("area", $area);
        $this->assign("user_ext", $user_ext);
        if (IS_POST) {
            if (empty($_POST['country']) || empty($_POST['city']) || empty($_POST['area'])) {
                $this->error("请选择所在地区");
            }
            $_POST['uid']       = session('uid');
            $_POST['user_area'] = "_{$_POST['country']}_{$_POST['city']}_{$_POST['area']}_";
            if ($this->Model->create()) {
                if ($this->Model->save()) {
                    $this->success("修改成功！", __SELF__);
                } else {
                    $this->error("修改失败！");
                }
            } else {
                $this->error($this->Model->getError());
            }
        } else {
            $this->assign("select_categorys", $select_categorys);
            $this->display();
        }
    }

    public function verified() {
        $uid = session('uid');
        if ($_POST) {
            $_POST['user_id']         = $uid;
            $_POST['status_verified'] = 0;
            $savePath                 = "/Public/Uploads/" . date("Ymd") . "/";
            $file                     = $this->upload($savePath);
            $_POST['idcard_img']      = $savePath . $file['idcard_img']['savename'];
            $this->Model = D('UserVerified');
            if ($this->Model->create()) {
                if ($this->Model->add()) {
                    $this->success("提交认证成功！");
                    exit;
                } else {
                    $this->error("提交认证失败！");
                }
            } else {
                $this->error($this->Model->getError());
            }
        }
        $ver     = M('UserVerified')->where("user_id={$uid}")->find();
        //推荐信息
        $roundDZ = _roundDazhe();
        $this->assign('rounddz', $roundDZ);
        $this->assign('ver', $ver);
        $this->display();
    }

    /**
     * 修改个人资料 
     */
    public function charge() {
        $uid  = session('uid');
        //会员信息
        $info = M('User')->find($uid);
        //商户会员信息
        if ($info['type'] == 'shop') {
            $info['shop'] = M('Shop')->where(array('user_id' => $uid))->find();
        }

        if (IS_POST) {
            $info['sex']      = t($_POST['sex']);
            $info['career']   = t($_POST['career']);
            $info['birthday'] = t($_POST['birthday']);
            $user             = M('User')->save($info);
            if ($info['type'] == 'shop') {
                $info['shop']['shop_name']    = t($_POST['shop_name']);
                $info['shop']['shop_address'] = t($_POST['shop_address']);
                $info['shop']['link_man']     = t($_POST['link_man']);
                $info['shop']['link_phone']   = t($_POST['link_phone']);
                $shop                         = M('Shop')->save($info['shop']);
            }
            if ($user >= 0 || $shop >= 0) {
                $this->success('修改成功');
                exit;
            } else {
                $this->error('修改失败, 请重试');
            }
        }
        $this->assign($info);
        $this->display();
    }

    public function ajax() {
        $uid = session('uid');
        $req = $_REQUEST['req'];
        switch ($req) {
            case 'setCareer':
                $content = '<form method="post" action="?login" id="career-form"><p><label>职业：<input type="text" name="career" id="career-form-data"></label></p></form>';
                exit($content);
                break;
            case 'dosetCareer':
                $career  = $_REQUEST['career'];
                $res     = D('User')->where(array('user_id' => $uid))->setField('career', $career);
                if ($res == true)
                    exit(1);
                else
                    exit(0);
                break;
        }
    }

}
